package com.example.demo.controller;


import com.example.demo.entiy.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserAuthorityService;
import com.example.demo.service.UserService;
import com.example.demo.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class LoginRegisterController {

    private static String testPassword;

    @Autowired
    UserService userService;

    @Autowired
    UserAuthorityService userAuthorityService;

    @GetMapping("/login")
    public String login() {
        return "/comm/login";
    }

    @GetMapping("/register")
    public String register() {
        return "/comm/register";
    }

    /**
     * 使用ajax检查用户名是否被使用
     *
     * @param username
     * @return
     */
    @GetMapping("/checkUsername")
    @ResponseBody
    public String checkUsername(String username) {
        String message = "";
        try {
            User user = userService.getUserByUsername(username);
            if (user != null) {
                message = "该用户名已被注册，请重新输入！";
            }
        } catch (Exception e) {
            System.out.println();
            System.out.println(e.getMessage());
            System.out.println();
        }
        return message;
    }

    @PostMapping("/checkFirstPassword")
    public void checkFirstPassword(String firstPassword) {
        testPassword = firstPassword;
    }


    @PostMapping("/checkSecondPassword")
    @ResponseBody
    public String checkSecondPassword(String secondPassword) {
        String message = "";
        if (testPassword.equals(secondPassword)) {
            message = "";
        } else {
            message = "两次密码输入不一致，请检查后输入！";
        }
        return message;
    }

    /**
     * 拿到注册信息进行检查并且插入数据库中
     *
     * @param user
     * @param model
     * @return
     */
    @PostMapping("/toRegister")
    public String toRegister(User user, Model model) {
        String address = "";
        Date date = new Date();
        String string = Tools.dateFormat(date);
        user.setCreated(string);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        String encode = encoder.encode(password);
        user.setPassword(encode);
        try {
            userService.insertUserByUser(user);
            User u = userService.getUserByUsername(user.getUsername());
            userAuthorityService.insertUserAuthorityByUserId(u.getId());
            address = "/comm/login";
        } catch (Exception e) {
            model.addAttribute("error", "注册失败！");
            address = "/comm/register";
            System.out.println(e.getMessage());
        }
        return address;
    }

}
